package kingwing.dc.core.view.keyword;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import kingwing.dc.core.model.Infos;
import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;
import kingwing.dc.core.orm.hibernate.CommonHibernateDao;
import kingwing.dc.core.sevice.HTMLProcessor;
import kingwing.dc.core.sevice.JSProcessor;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ResultItemsCollectorPipeline;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.Align;
import com.bstek.dorado.view.widget.base.Dialog;
import com.bstek.dorado.view.widget.base.FieldSet;
import com.bstek.dorado.view.widget.base.tab.TabControl;
import com.bstek.dorado.view.widget.form.TextArea;
import com.bstek.dorado.view.widget.grid.DataColumn;
import com.bstek.dorado.view.widget.grid.DataGrid;

@Component
public class SiteKeyWordPR {

	@Resource(name=CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao commonHibernateDao;
	
	/**
	 * 动态展示页面
	 * @param view
	 * @throws Exception
	 */
	public void onViewInit(View view) throws Exception{
		
		DataGrid dataGridInfos = (DataGrid) view.getViewElement("dataGridInfos");
		Dialog dialogInfos = (Dialog) view.getViewElement("dialogInfos");
		
		/*
		 * 判断管理员是否显示信息所属列
		 * 
		if (ContextHolder.getLoginUser().isAdministrator()) {
			DataColumn dataColumn = new DataColumn();
			dataColumn.setAlign(Align.center);
			dataColumn.setName("createUserId");
			dataColumn.setProperty("createUserId");
			dataGridInfos.addColumn(dataColumn);
		}
		*/
		
		//配置dataGridInfos
		DataColumn dataColumnRuleName = new DataColumn();
		dataColumnRuleName.setAlign(Align.center);
		dataColumnRuleName.setCaption("来源站点");
		dataColumnRuleName.setName("ruleName");
		dataColumnRuleName.setProperty("ruleName");
		dataGridInfos.addColumn(dataColumnRuleName);
		
		DataColumn dataColumnTitle = new DataColumn();
		dataColumnTitle.setAlign(Align.center);
		dataColumnTitle.setFilterable(false);
		dataColumnTitle.setCaption("标题");
		dataColumnTitle.setName("field1");
		dataColumnTitle.setProperty("field1");
		dataGridInfos.addColumn(dataColumnTitle);
		
		DataColumn dataColumnTime = new DataColumn();
		dataColumnTime.setFilterable(false);
		dataColumnTime.setAlign(Align.center);
		dataColumnTime.setCaption("日期");
		dataColumnTime.setName("field3");
		dataColumnTime.setProperty("field3");
		dataGridInfos.addColumn(dataColumnTime);
		
		/*------------------标题-------------------*/
		
		
		//建立TextArea
		TextArea textAreaTitle = new TextArea();
		textAreaTitle.setId("textAreaTitle");
		textAreaTitle.setWidth("100%");
		textAreaTitle.setEditable(false);
		textAreaTitle.setDataSet("dataSetInfos");
		textAreaTitle.setProperty("field1");
		//建立FieldSet
		FieldSet fieldSetTitle = new FieldSet();
		fieldSetTitle.setTags("field");
		fieldSetTitle.setId("fieldSetTitle");
		fieldSetTitle.setCaption("标题");
		fieldSetTitle.addChild(textAreaTitle);
		
		dialogInfos.addChild(fieldSetTitle);
		/*------------------正文-------------------*/
		
		//建立TextArea
		TextArea textAreaContent = new TextArea();
		textAreaContent.setId("textAreaContent");
		textAreaContent.setWidth("100%");
		textAreaContent.setHeight("100%");
		textAreaContent.setEditable(false);
		textAreaContent.setDataSet("dataSetInfos");
		textAreaContent.setProperty("field2");
		//建立FieldSet
		FieldSet fieldSetContent = new FieldSet();
		fieldSetContent.setTags("field");
		fieldSetContent.setId("fieldSetContent");
		fieldSetContent.setCaption("内容");
		fieldSetContent.setHeight("100%");
		fieldSetContent.addChild(textAreaContent);
		
		dialogInfos.addChild(fieldSetContent);
		
		
		FieldSet fieldSet = new FieldSet();
		fieldSet.setTags("field");
		fieldSet.setId("fieldSetFX");
		fieldSet.setCaption("分析(注：以下结果仅供参考)");
		fieldSet.setHeight("100%");
		TabControl tc = (TabControl) view.getViewElement("tabControl1");
		
		fieldSet.addChild(tc);
		
		dialogInfos.addChild(fieldSet);
		
	}
	
	
	/**
	 * 依据规则名称、关键字分页获取采集信息
	 * @param page
	 */
	@SuppressWarnings("unchecked")
	@DataProvider
	public void getInfosPage(Page<Infos> page,Map<String, Object> params ){
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Infos.class);
		Disjunction disjunction = Restrictions.disjunction();
		List<Rule> rules = new ArrayList<Rule>();
		String text = "";
		if (!params.isEmpty() && params!=null) {
			rules =  (List<Rule>) params.get("rule");
			text = params.get("text").toString();
		}
		
		if (rules != null) {
			for (Rule rule : rules) {
				String ruleName = rule.getRuleName();
				disjunction.add(Restrictions.eq("ruleName", ruleName));
			}
		}
		
		if (StringUtils.isNotBlank(text)) {
			detachedCriteria.add(Restrictions.or(Restrictions.ilike("field1", "%"+text+"%"),
					Restrictions.ilike("field2", "%"+text+"%")));
		}
		
		detachedCriteria.add(disjunction);
		detachedCriteria.add(Restrictions.eq("createUserId", ContextHolder.getLoginUserName()));
		detachedCriteria.addOrder(Order.desc("field3"));
		
		this.commonHibernateDao.pagingQuery(page, detachedCriteria);
	}
	
}
